
import { _decorator, Component, Node , sys} from 'cc';
import { GNetConst } from '../Constans/Conf1';
import GameMemoryStorage1 from '../Data/GameMemoryStorage1';
const { ccclass, property } = _decorator;

@ccclass('GameStorage')
export class GameStorage extends Component {

    static GameKey = GNetConst.SaveHead;

    public static Key(key:string){
        return this.GameKey + '_' + key;
    }

    public static GetRawKey(key:string){
        return key.replace(GameStorage.GameKey+"_","")
    }

    static getStorage(){
        return GameMemoryStorage1
        // return Const.isActiveMemoryTmp?GameMemoryStorageTmp: GameMemoryStorage1
    }

    static getInt(key:string, def:number = 0){
        let n = this.getStorage().getItem(this.Key(key));
        if (n !== null && n !== "") {
            return Number(n);
        }
        return def;
    }

    static setInt(key:string, num:number){
        key = this.Key(key);
        this.getStorage().setItem(key, num+"");
    }
    static setIntMemory(key:string,num:number){
        key = this.Key(key);
        this.getStorage().setItem(key, num+"");
    }

    static getBoolean(key:string, def:boolean = false){
        let n = this.getStorage().getItem(this.Key(key));
        if (n !== null&&n !== "") {
            return Number(n) == 1?true:false;
        }
        return def;
    }

    static setBoolean(key:string,is:boolean){
        key = this.Key(key);
        let v = is?1:0;
        this.getStorage().setItem(key,v+"");
    }
    static setBooleanMemory(key:string,is:boolean){
        key = this.Key(key);
        let v = is?1:0;
        this.getStorage().setItem(key,v+"");
    }

    static getString(key:string,def:string = ''){
        let n = this.getStorage().getItem(this.Key(key));
        if (n !== null&& n !== "") {
            return n;
        }
        return def;
    }
    //登录部分信息才用
    static getStringDisk(key:string,def:string = ''){
        let n = localStorage.getItem(this.Key(key));
        if (n !== null&& n !== "") {
            return n;
        }
        return def;
    }
    static getIntDisk(key:string,def:string = ''){
        let n = localStorage.getItem(this.Key(key));
        if (n !== null&& n !== "") {
            return Number(n);
        }
        return def;
    }
    static setStringDisk(key:string,val:string){
        localStorage.setItem(this.Key(key), val);
    }

    static setStringMemory(key:string,val:string){
        key = this.Key(key);
        this.getStorage().setItem(key,val);
        localStorage.setItem(key, val);
    }

    static setString(key:string,num:string){
        key = this.Key(key);
        this.getStorage().setItem(key,num);
        if (key == GameStorage.GameKey+'login_account') {
            return;
        }
    }


    static getObject(key:string,def:any = null){
        let n = this.getStorage().getItem(this.Key(key));
        if (n !== null&& n !== "") {
            try {
                let json = JSON.parse(n);
                return json || def;
            } catch (error) {
                return def;
            }
            
        }
        return def;
    }

    static setObject(key:string,obj:any){
        key = this.Key(key);
        let v = JSON.stringify(obj);
        this.getStorage().setItem(key,v);
    }
    static sendObject(key:string, obj:any){
        key = this.Key(key);
        let v = JSON.stringify(obj);
    }

    static setObjectMemory(key:string,obj:any){
        key = this.Key(key);
        let v = JSON.stringify(obj);
        this.getStorage().setItem(key,v);
    }

    static removeItem(key:string){
        key = this.Key(key);
        this.getStorage().removeItem(key);
    }

    static clear(){
        // console.log('清除数据:',localStorage.length)

        let list:string[] = [];

        for (let i = 0; i <  localStorage.length; i++) {
            let key:string =  localStorage.key(i)!; //获取本地存储的Key
            if (key.indexOf(this.GameKey) >=0 && key.indexOf('login') == -1 && key.indexOf('zzdl') == -1) {
                // cc.log("清理前检查key", key, key.indexOf(this.GameKey) )
                list.push(key);
            }
        }

        let keys = this.getStorage().keys();
        for (let i = 0; i <  keys.length; i++) {
            let key:string =  keys[i]; //获取本地存储的Key
            if (key.indexOf('login') == -1 && key.indexOf('zzdl') == -1) {
                // console.log("清除key:",key)
                list.push(key);
            }
        }

        for (let i = 0; i < list.length; i++) {
            const key = list[i];
            this.getStorage().removeItem(key);
            // this.removeItem(key);
        }
        //清空本地无用的缓存

        // localStorage.clear();
    }

    //老用户不用改为内存
    static clearByKey(gameKey:string){
        let list:string[] = [];
        for (let i = 0; i <  localStorage.length; i++) {
            let key:string =  localStorage.key(i)!; //获取本地存储的Key
            if (key.indexOf(gameKey) != -1) {
                list.push(key);
            }
        }
        for (let i = 0; i < list.length; i++) {
            const key = list[i];
            this.getStorage().removeItem(key);
        }
    }

    static getStringArrayByKey(str:string){
        // console.log("获取列表:",str)
        let list:any[] = [];
        let list2:any[] = [];
        let keys = this.getStorage().keys();
        for (let i = 0; i <  keys.length; i++) {
            const key:string =  keys[i]; //获取本地存储的Key
            //console.log("key:",key);
            if (key.indexOf(str) != -1) {
                let n = this.getStorage().getItem(key);
                // console.log("=====n:",n)
                list.push(n);
                list2.push(key)
            }
        }
        return {keys: list2, values: list};
    }

    static getObjectArrayByKey(str:string){
       // console.log("获取列表:",str)
        let list:any[] = [];
        let keys = this.getStorage().keys();
        for (let i = 0; i <  keys.length; i++) {
            const key:string =  keys[i]; //获取本地存储的Key
            //console.log("key:",key);
            if (key.indexOf(str) != -1) {
                let n = this.getStorage().getItem(key);
                //console.log("n:",n)
                let value = null;
                if (n !== null&& n !== "") {
                    try {
                        let json = JSON.parse(n);
                        value = json || null;
                    } catch (error) {
                        value = null;
                    }
                }
                if (value) {
                    list.push(value);
                }
                
            }
            
        }
        return list;
    }

    //老用户兼容使用，不改动，以及GM也使用这个接口上传数据到云端
    static getAll(isFirst:boolean = false){
        let jsonData:any = {}
        for (let i = 0; i <  localStorage.length; i++) {
            const key:string =  localStorage.key(i)!; //获取本地存储的Key
            if (key.indexOf('login') != -1) {
                continue;
            }
            if (isFirst) {
                let oldKey = key;
                let newKey = key.replace('zzdl',GameStorage.GameKey);
                jsonData[newKey] = localStorage.getItem(oldKey)
            }
            else{
                if (key.indexOf('zzdl') != -1) {
                    continue;
                }
                jsonData[key]=localStorage.getItem(key)
            }
            
            // console.log(key);
            // console.log(localStorage.getItem(key));//所有value
            // console.log("---------------------------------");
        }

        if (isFirst) {
            let spliceDatas:any = {};
            for (const key in jsonData) {
                if (Object.prototype.hasOwnProperty.call(jsonData, key)) {
                    const value = jsonData[key];
                    // 大数据拆分.
                    // 1.装备
                    // 2.玄天宝录
                    // console.log("老用户数据处理key:",key,value);
                    switch (key) {
                        case GameStorage.GameKey+'_sect_xtbls':
                            {
                                let datas = JSON.parse(value);
                                // console.log("xtbl datas:",datas.length,datas);
                                for (let i = 0; i < datas.length; i++) {
                                    const xtbl = datas[i];
                                    let data = {
                                        id:xtbl.id,
                                        star:xtbl.star,
                                        debris:xtbl.debris
                                    }
                                    // GameStorage.setObject('SectXtbl_'+xtbl.id,data);
                                    spliceDatas[GameStorage.GameKey + '_SectXtbl_'+xtbl.id] = JSON.stringify(data);
                                }
                            }
                            break;
                        case GameStorage.GameKey+'_pack_equips':
                            {
                                let datas = JSON.parse(value);
                                // console.log("equip datas:",datas.length,datas);
                                for (let i = 0; i < datas.length; i++) {
                                    const equip = datas[i];
                                    let data = {
                                        id:equip.id,
                                        index:equip.index,
                                        level:equip.level,
                                        star:equip.star,
                                        partner:equip.partner,
                                        score:equip.score,
                                        skill_skr:equip.skill_skr,
                                        skill_skr_attr:equip.skill_skr_attr,
                                        skill_star:equip.skill_star,
                                        skill_star_attr:equip.skill_star_attr,
                                    };
                                    // GameStorage.setObject('pack_equip_'+equip.index,data);
                                    spliceDatas[GameStorage.GameKey + 'pack_equip_' + equip.index] = JSON.stringify(data);
                                }
                            }
                            break;    
                        default:
                            {
                                localStorage.setItem(key,value);
                            }
                            break;
                    }
                    
                }
            }
            delete jsonData['zzdl_sect_xtbls'];
            delete jsonData['zzdl_pack_equips'];

            // console.log("spliceDatas:",spliceDatas);

            for (const key in spliceDatas) {
                const data = spliceDatas[key];
                jsonData[key] = data;
            }
        }

        return jsonData
    }

    static async setAll(jsonData:any){
        // console.log("11111覆盖数据")
        // await this.clear();
        // console.log("覆盖前清除所有数据........",jsonData)
        
        for(let key in jsonData){
            this.getStorage().setItem(key, jsonData[key])
        }
    }

}

